package com.example.q_kang.hhh.order;

import com.bulong.rudeness.RudenessScreenHelper;
import com.example.q_kang.hhh.R;
import com.example.q_kang.hhh.order.match.BuyDakuanMatchDetailActivity;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.base.MVPFragment;
import com.sskj.base.adapter.slimadapter.IViewInjector;
import com.sskj.base.adapter.slimadapter.SlimAdapter;
import com.sskj.base.adapter.slimadapter.SlimInjector;
import com.sskj.base.bean.BuyDakuanListBean;
import com.sskj.base.bean.BuyPiPeiListBean;
import com.sskj.base.mvchelper.ModelRx2DataSource;
import com.sskj.base.utils.DateUtil;
import com.sskj.base.view.DividerLineItemDecoration;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

/**
 * @author Hey
 * Create at  2019/04/13
 */
public class BuyWaitDaKuanFragment extends MVPFragment<BuyWaitDaKuanPresenter> {

    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private SlimAdapter<BuyDakuanListBean.BuyDakuanBean> slimAdapter;
    private MVCCoolHelper<List<BuyDakuanListBean.BuyDakuanBean>> mvcHelper;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_buy_wai_t_dakuan;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mvcHelper != null) {
            mvcHelper.refresh();
        }
    }

    @Override
    public BuyWaitDaKuanPresenter getPresenter() {
        return new BuyWaitDaKuanPresenter();
    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(getActivity(), 20))
                .setDividerColor(ContextCompat.getColor(getContext(), R.color.transparent)));
        slimAdapter = SlimAdapter.create().register(R.layout.item_buy_wait_dakuan, new SlimInjector<BuyDakuanListBean.BuyDakuanBean>() {
            @Override
            public void onInject(BuyDakuanListBean.BuyDakuanBean data, IViewInjector injector) {
                injector.clicked(R.id.tv_pay, view -> BuyDakuanMatchDetailActivity.start(getActivity(), data.getId()))
                        .text(R.id.tv_order, data.getAdd_time())
                        .text(R.id.tv_money, data.getPd_price())
                        .image(R.id.iv_type, data.getOrder_type().equals("1") ? R.mipmap.icon_main :
                                data.getOrder_type().equals("2") ? R.mipmap.icon_fu1 :
                                        data.getOrder_type().equals("3") ? R.mipmap.icon_fu2 :
                                                R.mipmap.icon_fu3)
                        .text(R.id.tv_date, DateUtil.getDateFormat(Long.valueOf(data.getAdd_time()) * 1000, DateUtil.PATTERN_J));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        ModelRx2DataSource<BuyDakuanListBean.BuyDakuanBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<BuyDakuanListBean.BuyDakuanBean>)
                        page -> mPresenter.getBuyWaitDaKuan(page), 10);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();

    }


    @Override
    public void initData() {

    }


    public static BuyWaitDaKuanFragment newInstance() {
        BuyWaitDaKuanFragment fragment = new BuyWaitDaKuanFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


}
